package com.dmm.vod.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dmm.vo.vod.CourseFormVo;
import com.dmm.vo.vod.CoursePublishVo;
import com.dmm.vo.vod.CourseQueryVo;
import com.dmm.vod.entity.Course;
import com.dmm.vod.entity.CourseDescription;
import com.dmm.vod.entity.Subject;
import com.dmm.vod.entity.Teacher;
import com.dmm.vod.mapper.CourseMapper;
import com.dmm.vod.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.DispatcherServlet;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author dmm
 * @since 2022-09-07
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    CourseMapper courseMapper;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private SubjectService subjectService;

    @Autowired
    CourseDescriptionService courseDescriptionService;

    @Autowired
    ChapterService chapterService;

    @Autowired
    VideoService videoService;


    @Override
    public Map<String, Object> findPage(Page<Course> pages, CourseQueryVo courseQueryVo) {

        //获取条件值
        String title = courseQueryVo.getTitle();//名称
        Long subjectId = courseQueryVo.getSubjectId();//二级分类
        Long subjectParentId = courseQueryVo.getSubjectParentId();//一级分类
        Long teacherId = courseQueryVo.getTeacherId();//讲师
        //封装条件
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(title)) {
            wrapper.like("title",title);
        }
        if(!StringUtils.isEmpty(subjectId)) {
            wrapper.eq("subject_id",subjectId);
        }
        if(!StringUtils.isEmpty(subjectParentId)) {
            wrapper.eq("subject_parent_id",subjectParentId);
        }
        if(!StringUtils.isEmpty(teacherId)) {
            wrapper.eq("teacher_id",teacherId);
        }

        Page<Course> coursePage = courseMapper.selectPage(pages, wrapper);

        long totalCount = coursePage.getTotal();//总记录数
        long totalPage = coursePage.getPages();//总页数
        long currentPage = coursePage.getCurrent();//当前页
        long size = coursePage.getSize();//每页记录数
        //每页数据集合
        List<Course> records = coursePage.getRecords();
        //遍历封装讲师和分类名称
        records.stream().forEach(item -> {
            this.getTeacherOrSubjectName(item);
        });
//封装返回数据
        Map<String,Object> map = new HashMap<>();
        map.put("totalCount",totalCount);
        map.put("totalPage",totalPage);
        map.put("records",records);
        return map;
    }

    //添加课程基本信息
    @Override
    public Long saveCourseInfo(CourseFormVo courseFormVo) {

        Course course=new Course();
        BeanUtils.copyProperties(courseFormVo,course);
        courseMapper.insert(course);

        //保存课程详情信息
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setDescription(courseFormVo.getDescription());
        courseDescription.setCourseId(course.getId());
        courseDescriptionService.save(courseDescription);

        return course.getId();
    }

    //根据id查询课程信息
    @Override
    public CourseFormVo getCourseFormVoById(Long id) {

        Course course = courseMapper.selectById(id);
        CourseDescription byId = courseDescriptionService.getById(id);
        CourseFormVo courseFormVo =new CourseFormVo();
        BeanUtils.copyProperties(course,courseFormVo);
        if(byId != null){
            courseFormVo.setDescription(byId.getDescription());
        }
        return courseFormVo;
    }


    //修改课程信息
    @Override
    public Long updateCourseById(CourseFormVo courseFormVo) {

        Course course=new Course();
        BeanUtils.copyProperties(courseFormVo,course);
        int i = courseMapper.updateById(course);
        QueryWrapper<CourseDescription> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("course_id",course.getId());
        CourseDescription courseDescription = courseDescriptionService.getOne(queryWrapper);
        courseDescription.setDescription(courseFormVo.getDescription());
        boolean update = courseDescriptionService.updateById(courseDescription);

        return course.getId();
    }

    //根据id获取课程发布信息
    @Override
    public CoursePublishVo getCoursePublishVo(Long id) {

        return courseMapper.selectCoursePublishVoById(id);
    }


    //删除课程
    @Override
    public void removeCourseById(Long id) {

        //根据课程id删除小节
        videoService.removeVideoByCourseId(id);
        //根据课程id删除章节
        chapterService.removeChapterByCourseId(id);
        //根据课程id删除描述
        courseDescriptionService.removeById(id);
        //根据课程id删除课程
        courseMapper.deleteById(id);

    }

    private Course getTeacherOrSubjectName(Course course) {
        //查询讲师名称
        Teacher teacher = teacherService.getById(course.getTeacherId());
        if(teacher != null) {
            course.getMap().put("teacherName",teacher.getName());
        }
        //查询分类名称
        Subject subjectOne = subjectService.getById(course.getSubjectParentId());
        if(subjectOne != null) {
            course.getMap().put("subjectParentTitle",subjectOne.getTitle());
        }
        Subject subjectTwo = subjectService.getById(course.getSubjectId());
        if(subjectTwo != null) {
            course.getMap().put("subjectTitle",subjectTwo.getTitle());
        }
        return course;
    }
}
